package com.AFH.test;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

public class ViewSwitcher extends Activity {
	/** Called when the activity is first created. */
	ProgressBar LoadingBar = null;
	View currentView;
	GameView gv;
	WelcomeView wv;
	AboutView av;
	Boolean wantSound = true;
	float volume;

	Handler AfhHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {

			// TODO Auto-generated method stub
			// super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				wv.WelcomeMicPool.release();
				gv = new GameView(ViewSwitcher.this);
				setContentView(gv);
				currentView = gv;
				break;
			case 1:

//				if(ViewSwitcher.this.wantSound){
//					wv.playSound(1, -1);
//				}
//				if(wv==null){
//					wv = new WelcomeView(ViewSwitcher.this);
//				}
				wv = new WelcomeView(ViewSwitcher.this);
				setContentView(wv);
				currentView = wv;
				break;
			case 2:
				
				wv.WelcomeMicPool.release();
				android.os.Process.killProcess(android.os.Process.myPid());
				break;
			case 3:
				wv.WelcomeMicPool.release();
				av = new AboutView(ViewSwitcher.this);
				setContentView(av);
				currentView = av;
				break;
			default:
				break;

			}
		}

	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		// setContentView(wv);
		AudioManager am=(AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
		float streamVolumeCurrent = am.getStreamVolume(AudioManager.STREAM_MUSIC);   
	    float streamVolumeMax = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);       
	    volume = streamVolumeCurrent / streamVolumeMax;  
	    
		setContentView(R.layout.mypage_left);
		wv = new WelcomeView(ViewSwitcher.this);
		// setContentView(R.layout.main);
		LoadingBar = (ProgressBar) findViewById(R.id.progress_horizontal_left);
		LoadingBar.setMax(100);
		LoadingBar.setProgress(0);
		LoadingBar.setIndeterminate(false);
	
		new Thread() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				BitmapManager.LoadWelcomeResource(getResources());
				LoadingBar.setProgress(20);
				BitmapManager.LoadMonsterResource(getResources());
				LoadingBar.setProgress(40);
				BitmapManager.LoadGameUIResource(getResources());
				LoadingBar.setProgress(50);
				BitmapManager.LoadbackgroundResource(getResources());
				LoadingBar.setProgress(60);
				BitmapManager.LoadAttackResource(getResources());
				LoadingBar.setProgress(70);
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				BitmapManager.LoadTowerResource(getResources());

				LoadingBar.setProgress(90);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				LoadingBar.setProgress(100);
				AfhHandler.sendEmptyMessage(1);
			}
		}.start();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		   if(keyCode == KeyEvent.KEYCODE_BACK){
			 
			   if(currentView == wv ){
				   AfhHandler.sendEmptyMessage(2);
			   }
			   if(currentView == gv){
				    gv.KeyDown(keyCode, event);
			   }
           return true;
       }
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_UP) {
			int PosX = (int) event.getX();
			int PosY = (int) event.getY();
			if (currentView == wv) {
				return wv.touchEvent(PosX, PosY);
			}
			if (currentView == gv) {
				return gv.gameTouch(PosX, PosY);
			}
			if (currentView == av) {
				return av.gameTouch(PosX, PosY);
			}

		}
		 

		// TODO Auto-generated method stub
		return true;
	}

}